package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 创建活动请求DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "创建活动请求参数")
public class CreateActivityRequest {

    @NotBlank(message = "活动标题不能为空")
    @Size(min = 2, max = 100, message = "活动标题长度在2-100字符之间")
    @Schema(description = "活动标题", example = "2024春季科技创新营", requiredMode = Schema.RequiredMode.REQUIRED)
    private String title;

    @NotBlank(message = "活动描述不能为空")
    @Size(max = 2000, message = "活动描述长度不能超过2000字符")
    @Schema(description = "活动详细描述", example = "面向中小学生的科技创新实践活动")
    private String description;

    @Schema(description = "基地ID", example = "1")
    private Long baseId;

    @Schema(description = "基地名称", example = "科技创新基地")
    private String baseName;

    @NotBlank(message = "活动地址不能为空")
    @Size(max = 500, message = "活动地址长度不能超过500字符")
    @Schema(description = "活动详细地址", example = "北京市海淀区中关村大街1号")
    private String address;

    @NotNull(message = "开始日期不能为空")
    @FutureOrPresent(message = "开始日期不能是过去日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动开始日期", example = "2024-03-01", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDate startDate;

    @NotNull(message = "结束日期不能为空")
    @FutureOrPresent(message = "结束日期不能是过去日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动结束日期",example = "2024-03-07", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDate endDate;

    @PositiveOrZero(message = "总天数不能为负数")
    @Schema(description = "活动总天数", example = "7")
    private Integer totalDays;

    @JsonFormat(pattern = "HH:mm:ss")
    @Schema(description = "每日开始时间", example = "09:00:00")
    private LocalTime startTime;

    @NotNull(message = "报名截止时间不能为空")
    @Future(message = "报名截止时间必须是未来时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "报名截止时间", example = "2024-02-28 18:00:00", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime registrationDeadline;

    @FutureOrPresent(message = "发布时间不能是过去时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "活动发布时间", example = "2024-02-20 10:00:00")
    private LocalDateTime publishTime;

    @NotBlank(message = "目标受众不能为空")
    @Size(max = 200, message = "目标受众描述长度不能超过200字符")
    @Schema(description = "目标受众描述", example = "中小学生、科技爱好者")
    private String targetAudience;

    @NotNull(message = "是否免费不能为空")
    @Schema(description = "是否免费活动", example = "true")
    @Builder.Default
    private Boolean isFree = true;  // 添加默认值

    @NotNull(message = "最大参与人数不能为空")
    @Min(value = 1, message = "最大参与人数至少为1")
    @Max(value = 10000, message = "最大参与人数不能超过10000")
    @Schema(description = "最大参与人数", example = "100", requiredMode = Schema.RequiredMode.REQUIRED)
    @Builder.Default
    private Integer maxParticipants = 50;  // 添加默认值

    @PositiveOrZero(message = "当前参与人数不能为负数")
    @Schema(description = "当前参与人数", example = "0")
    @Builder.Default
    private Integer currentParticipants = 0;  // 正确使用@Builder.Default

    @NotNull(message = "创建人ID不能为空")
    @Positive(message = "创建人ID必须是正整数")
    @Schema(description = "创建人用户ID", example = "123", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long creatorId;

    // 兼容性字段
    @Schema(description = "活动类型", example = "科技创新")
    private String category;

    @Deprecated
    @Schema(description = "活动名称（已废弃，请使用title）", example = "2024春季科技创新营")
    private String activityName;

    @Schema(description = "教室/场地ID", example = "5")
    private Long classroomId;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "特定活动日期", example = "2024-03-01")
    private LocalDate specificDate;

    @Schema(description = "时间节次ID", example = "3")
    private Long timeSlotId;

    @Schema(description = "教师ID", example = "45")
    private Long teacherId;

    @Schema(description = "学期ID", example = "2")
    private Long termId;

    @Schema(description = "班级ID", example = "10")
    private Long classId;

    /**
     * 验证日期逻辑
     */
    public boolean isValidDateRange() {
        if (startDate == null || endDate == null) {
            return false;
        }
        return !endDate.isBefore(startDate);
    }

    /**
     * 验证报名截止时间逻辑
     */
    public boolean isValidRegistrationDeadline() {
        if (registrationDeadline == null || startDate == null) {
            return false;
        }
        return registrationDeadline.isBefore(startDate.atStartOfDay());
    }

    /**
     * 自动计算总天数
     */
    public Integer calculateTotalDays() {
        if (startDate != null && endDate != null && !endDate.isBefore(startDate)) {
            return (int) java.time.temporal.ChronoUnit.DAYS.between(startDate, endDate) + 1;
        }
        return totalDays != null ? totalDays : 0;
    }
}
